package com.xinqi.modules.stats.score.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.course.course.client.RemoteCourseClient;
import com.xinqi.modules.course.course.dto.rsp.CourseResultDTO;
import com.xinqi.modules.stats.score.convert.CourseTotalScoreContentSonConvert;
import com.xinqi.modules.stats.score.domain.CourseTotalScoreContentSonEntity;
import com.xinqi.modules.stats.score.dto.req.*;
import com.xinqi.modules.stats.score.dto.rsp.CourseTotalScoreContentSonResultDTO;
import com.xinqi.modules.stats.score.mapper.CourseTotalScoreContentSonMapper;
import com.xinqi.modules.stats.score.service.CourseTotalScoreContentSonService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;


/**
 * 总成绩课程子成绩项目配置表服务层实现
 *
 * @author: system
 * @date: 2023/03/26
 */
@Service("courseTotalScoreContentSonService")
@RequiredArgsConstructor
public class CourseTotalScoreContentSonServiceImpl extends BaseServiceImpl<CourseTotalScoreContentSonMapper, CourseTotalScoreContentSonEntity> implements CourseTotalScoreContentSonService {

    private final CourseTotalScoreContentSonConvert courseTotalScoreContentSonConvert;

    private final RemoteCourseClient remoteCourseClient;

    @Override
    public List<CourseTotalScoreContentSonResultDTO> findTotalScoreContentSon(CourseTotalScoreContentSonClassDTO dto) {
        return this.findReleaseCourseContent(dto);
    }

    private List<CourseTotalScoreContentSonResultDTO> findReleaseCourseContent(CourseTotalScoreContentSonClassDTO dto) {
        List<CourseTotalScoreContentSonResultDTO> baseResultDTOS = this.baseMapper.findReleaseCourseContent(dto);
        LambdaQueryWrapper<CourseTotalScoreContentSonEntity> wrapper = Queries.lambda();
        wrapper.eq(CourseTotalScoreContentSonEntity::getClassId, dto.getClassId());
        wrapper.eq(CourseTotalScoreContentSonEntity::getType, dto.getType());
        List<CourseTotalScoreContentSonResultDTO> sonResultDTOS = this.baseMapper.selectDtoList(wrapper, courseTotalScoreContentSonConvert::convert);
        baseResultDTOS.forEach(item -> {
            if (checkStatus(sonResultDTOS, item)) {
                item.setStatus(1);
            } else {
                item.setStatus(0);
            }
        });
        return baseResultDTOS;
    }

    //判断是否选中
    private boolean checkStatus(List<CourseTotalScoreContentSonResultDTO> sonResultDTOS, CourseTotalScoreContentSonResultDTO son) {
        AtomicBoolean flag = new AtomicBoolean(false);
        CourseTotalScoreContentSonResultDTO selSonResult = null;
        sonResultDTOS.stream().filter(item -> item.getContentId().equals(son.getContentId()) && item.getSliceId().equals(son.getSliceId())).forEach(item -> {
            flag.set(true);
        });
        if (selSonResult != null) {
            sonResultDTOS.remove(selSonResult);
        }
        return flag.get();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setTotalScoreContentSon(CourseTotalScoreContentSonSetDTO dto) {
        CourseTotalScoreContentSonClassDTO sonClassDTO = new CourseTotalScoreContentSonClassDTO();
        sonClassDTO.setClassId(dto.getClassId());
        sonClassDTO.setType(dto.getType());
        this.baseMapper.deleteCourseContentSon(sonClassDTO);
        CourseResultDTO courseEntity = remoteCourseClient.findCourse(dto.getClassId()).getData();
        List<CourseTotalScoreContentSonEntity> list = dto.getList().stream().filter(v -> v.getStatus() == 1).map(item -> {
            CourseTotalScoreContentSonEntity entity = courseTotalScoreContentSonConvert.update(item);
            entity.setCourseId(courseEntity.getId());
            entity.setClassId(dto.getClassId());
            entity.setType(dto.getType());
            return entity;
        }).collect(Collectors.toList());
        if (Objects.isNull(list) || list.size() == 0) {
            return true;
        }
        return this.saveOrUpdateBatch(list);
    }

    @Override
    public List<CourseTotalScoreContentSonResultDTO> findExistTotalScoreContentSon(CourseTotalScoreContentSonClassDTO dto) {
        LambdaQueryWrapper<CourseTotalScoreContentSonEntity> wrapper = Queries.lambda();
        wrapper.eq(CourseTotalScoreContentSonEntity::getClassId, dto.getClassId());
        wrapper.eq(CourseTotalScoreContentSonEntity::getType, dto.getType());
        wrapper.eq(CourseTotalScoreContentSonEntity::getStatus, 1);
        return this.baseMapper.selectDtoList(wrapper, courseTotalScoreContentSonConvert::convert);
    }

    @Override
    public boolean syncStudentTypeContent(CourseTotalScoreStudentCreateAllDTO dto) {
        return false;
    }
}
